﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace WindowsGame1
{
    class GameplayState : BaseState
    {
        Texture2D m_texBackGround;
        Texture2D m_texGround;
        Camera m_Camera = new Camera();
        HUD m_Hud = new HUD();

        ContentManager Content;

        public GameplayState(Game1 _game)
        {
            Content = new ContentManager(_game.Services, "Content");
        }

        KeyboardState m_LastKeyState;

        public void Enter()
        {
            Content.RootDirectory = "Content";
            m_texBackGround = Content.Load<Texture2D>("background");
            m_texGround = Content.Load<Texture2D>("ground");

            Player tempPlayer = new Player();
            tempPlayer.m_Texture = Content.Load<Texture2D>("HumanTemplate");
            tempPlayer.m_Position = new Vector2(100, 100);
            m_Camera.m_Target = tempPlayer;

            Equip tempHair = new Equip();
            tempHair.m_Texture = Content.Load<Texture2D>("Equips/Hair/Hair002");
            tempHair.m_cColor = Color.Green;
            tempPlayer.m_lEquips.Add(tempHair);

            Equip tempEyes = new Equip();
            tempEyes.m_Texture = Content.Load<Texture2D>("Equips/Eyes/Eyes001");
            tempEyes.m_cColor = Color.White;
            tempPlayer.m_lEquips.Add(tempEyes);

            Equip tempLegs = new Equip();
            tempLegs.m_Texture = Content.Load<Texture2D>("Equips/Legs/Legs001");
            tempLegs.m_cColor = Color.Khaki;
            tempPlayer.m_lEquips.Add(tempLegs);

            Equip tempChest = new Equip();
            tempChest.m_Texture = Content.Load<Texture2D>("Equips/Chest/Chest001");
            tempChest.m_cColor = Color.LightBlue;
            tempPlayer.m_lEquips.Add(tempChest);

            Equip tempShoulder = new Equip();
            tempShoulder.m_Texture = Content.Load<Texture2D>("Equips/Shoulder/Shoulder001");
            tempShoulder.m_cColor = Color.DarkKhaki;
            //tempPlayer.m_lEquips.Add(tempShoulder);

            Equip tempWeapon = new Equip();
            tempWeapon.m_Texture = Content.Load<Texture2D>("Equips/Weapon/Weapon001");
            tempWeapon.m_cColor = Color.Silver;
            tempPlayer.m_lEquips.Add(tempWeapon);

            ObjectManager.Instance.m_ObjectList.Add(tempPlayer);

            //SceneObject tempRock = new SceneObject();
            //tempRock.m_Texture = Content.Load<Texture2D>("rock");
            //ObjectManager.Instance.m_ObjectList.Add(tempRock);

            //tempRock = new SceneObject();
            //tempRock.m_Texture = Content.Load<Texture2D>("rock");
            //tempRock.m_Position.X += 128;
            //ObjectManager.Instance.m_ObjectList.Add(tempRock);

            //Particle tempPart = new Particle();
            //tempPart.m_Texture = GlobalContentManager.Instance.Texture("Glow");
            //Emitter tempEmitter = new Emitter(10, 1.0f, tempPart);
            //tempEmitter.m_Position = new Vector2(500, 844);
            //ObjectManager.Instance.m_ObjectList.Add(tempEmitter);

            LevelManager.Instance.Load("Test1.pmp");
            //MediaPlayer.IsRepeating = true;
            //MediaPlayer.Play(GlobalContentManager.Instance.Music("Music/Jingle001"));

            //Slime tempEnemy = new Slime();
            //tempEnemy.m_Position = new Vector2(782, 300);
            //ObjectManager.Instance.m_ObjectList.Add(tempEnemy);

            m_Hud.m_fMainFont = Content.Load<SpriteFont>("Eichante");


        }

        public void Exit()
        {
            Content.Unload();
        }

        public void Update(float _delta)
        {
            ObjectManager.Instance.Update(_delta, m_LastKeyState);

            m_Camera.Update(_delta);

            m_LastKeyState = Keyboard.GetState();
        }

        public void Render(SpriteBatch _spriteBatch)
        {
            _spriteBatch.Begin();

            _spriteBatch.Draw(GlobalContentManager.Instance.Texture("pixel"),
                          new Rectangle((int)-m_Camera.m_Position.X, (int)-m_Camera.m_Position.Y,
                                        (int)(LevelManager.Instance.m_TileSize.X * LevelManager.Instance.m_MapSize.X),
                                        (int)(LevelManager.Instance.m_MapSize.Y * LevelManager.Instance.m_TileSize.Y)),
                          Color.CornflowerBlue);

            ObjectManager.Instance.Render(_spriteBatch, m_Camera);

            m_Hud.Render(_spriteBatch);

            _spriteBatch.End();
        }

    }
}
